package com.yxx.sys.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.digest.MD5;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yxx.sys.common.Result;
import com.yxx.sys.domain.AppUser;
import com.yxx.sys.service.AppUserService;
import com.yxx.sys.vo.AppUserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yxx
 * @since 2022-06-10
 */
@RestController
@RequestMapping("/sys/app-user")
@Api("app端用户控制器")
public class AppUserController {

  @Autowired
  AppUserService appUserService;

  @PostMapping("/addUser")
  @ApiOperation("创建用户")
  public Result addUser(AppUserVo userVo){
    AppUser user = new AppUser();
    BeanUtil.copyProperties(userVo,user);
    user.setPasswd(SecureUtil.md5(user.getPasswd()));
    boolean succ = appUserService.save(user);
    if(!succ) return Result.ADD_REEOR;
    return Result.ADD_SUCCESS;
  }

  @PostMapping("/modifyUser")
  @ApiOperation("修改用户信息")
  public Result modifyUser(AppUserVo appUserVo){
    AppUser appUser = appUserService.getOne(new QueryWrapper<AppUser>().eq("uid", appUserVo.getUid()).eq("passwd", SecureUtil.md5(appUserVo.getPasswd())));
    if(appUser==null) return new Result(-1,"用户不存在");
    BeanUtil.copyProperties(appUserVo,appUser);
    boolean succ = appUserService.updateById(appUser);
    if(!succ) return Result.ADD_REEOR;
    return Result.ADD_SUCCESS;
  }

  @PostMapping("/removeUser")
  @ApiOperation("删除用户")
  public Result removeUser(Integer id){
    boolean succ = appUserService.removeById(id);
    if(!succ) return Result.DELETE_REEOR;
    return Result.DELETE_SUCCESS;
  }

}
